﻿using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using LibHuman;
namespace Human
{

	public class frm_intokhaibhxh : System.Windows.Forms.Form
	{
        AccessData mDB = new AccessData();
        Language lan = new Language();
		private DataSet ds =new DataSet();
        private DataSet ds_hoten = new DataSet();
        string s_bophan="",user_dn="", sql = "",m_user="",sql1="",sql2="";
		private bool bClear=true;
        string ngay = "", thang = "", nam = "", ngayin = "",s_mmyy="";
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.Button butXem;
		private System.Windows.Forms.Button butIn;
        private System.Windows.Forms.Button butThoat;
        private LibList.List lst_bhxh;
        private TextBox txt_manv;
        private TextBox txt_hoten;
        private Label label3;
        private Label label1;
		private System.ComponentModel.Container components = null;

		public frm_intokhaibhxh(string s_makp,string s_userdn)
		{	
			InitializeComponent();
            lan.Read_Language_to_Xml(this.Name.ToString(), this);
            lan.Changelanguage_to_English(this.Name.ToString(), this);
            s_bophan = s_makp;
            user_dn = s_userdn;
		}

		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(frm_intokhaibhxh));
            this.label2 = new System.Windows.Forms.Label();
            this.butThoat = new System.Windows.Forms.Button();
            this.butIn = new System.Windows.Forms.Button();
            this.butXem = new System.Windows.Forms.Button();
            this.lst_bhxh = new LibList.List();
            this.txt_manv = new System.Windows.Forms.TextBox();
            this.txt_hoten = new System.Windows.Forms.TextBox();
            this.label3 = new System.Windows.Forms.Label();
            this.label1 = new System.Windows.Forms.Label();
            this.SuspendLayout();
            // 
            // label2
            // 
            this.label2.Location = new System.Drawing.Point(66, 69);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(45, 13);
            this.label2.TabIndex = 11;
            this.label2.Text = "Họ tên :";
            this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // butThoat
            // 
            this.butThoat.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.butThoat.Image = global::Human.Properties.Resources.close_r;
            this.butThoat.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.butThoat.Location = new System.Drawing.Point(181, 112);
            this.butThoat.Name = "butThoat";
            this.butThoat.Size = new System.Drawing.Size(65, 28);
            this.butThoat.TabIndex = 4;
            this.butThoat.Text = "Thoát";
            this.butThoat.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            this.butThoat.Click += new System.EventHandler(this.butThoat_Click);
            // 
            // butIn
            // 
            this.butIn.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.butIn.Image = global::Human.Properties.Resources.t_printer;
            this.butIn.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.butIn.Location = new System.Drawing.Point(110, 112);
            this.butIn.Name = "butIn";
            this.butIn.Size = new System.Drawing.Size(65, 28);
            this.butIn.TabIndex = 5;
            this.butIn.Text = "   &In";
            this.butIn.Click += new System.EventHandler(this.butIn_Click);
            // 
            // butXem
            // 
            this.butXem.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.butXem.Image = global::Human.Properties.Resources.t_search;
            this.butXem.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.butXem.Location = new System.Drawing.Point(39, 112);
            this.butXem.Name = "butXem";
            this.butXem.Size = new System.Drawing.Size(65, 28);
            this.butXem.TabIndex = 3;
            this.butXem.Text = "    &Xem";
            this.butXem.Click += new System.EventHandler(this.butXem_Click);
            // 
            // lst_bhxh
            // 
            this.lst_bhxh.ColumnCount = 0;
            this.lst_bhxh.FormattingEnabled = true;
            this.lst_bhxh.Location = new System.Drawing.Point(17, 146);
            this.lst_bhxh.MatchBufferTimeOut = 1000;
            this.lst_bhxh.MatchEntryStyle = AsYetUnnamed.MatchEntryStyle.FirstLetterInsensitive;
            this.lst_bhxh.Name = "lst_bhxh";
            this.lst_bhxh.Size = new System.Drawing.Size(75, 17);
            this.lst_bhxh.TabIndex = 12;
            this.lst_bhxh.TextIndex = -1;
            this.lst_bhxh.TextMember = null;
            this.lst_bhxh.ValueIndex = -1;
            this.lst_bhxh.Visible = false;
            this.lst_bhxh.KeyDown += new System.Windows.Forms.KeyEventHandler(this.lst_bhxh_KeyDown);
            // 
            // txt_manv
            // 
            this.txt_manv.Enabled = false;
            this.txt_manv.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.txt_manv.Location = new System.Drawing.Point(17, 85);
            this.txt_manv.Name = "txt_manv";
            this.txt_manv.Size = new System.Drawing.Size(50, 21);
            this.txt_manv.TabIndex = 1;
            this.txt_manv.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.txt_manv_KeyPress);
            this.txt_manv.Validated += new System.EventHandler(this.txt_manv_Validated);
            this.txt_manv.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txt_manv_KeyDown);
            // 
            // txt_hoten
            // 
            this.txt_hoten.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.txt_hoten.Location = new System.Drawing.Point(69, 85);
            this.txt_hoten.Name = "txt_hoten";
            this.txt_hoten.Size = new System.Drawing.Size(194, 21);
            this.txt_hoten.TabIndex = 2;
            this.txt_hoten.TextChanged += new System.EventHandler(this.txt_hoten_TextChanged);
            this.txt_hoten.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txt_hoten_KeyDown);
            // 
            // label3
            // 
            this.label3.Location = new System.Drawing.Point(14, 69);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(43, 13);
            this.label3.TabIndex = 15;
            this.label3.Text = "Mã NV:";
            this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // label1
            // 
            this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 11.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label1.ForeColor = System.Drawing.Color.DarkBlue;
            this.label1.Location = new System.Drawing.Point(14, 9);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(248, 60);
            this.label1.TabIndex = 16;
            this.label1.Text = "IN TỜ KHAI BẢO HIỂM XÃ HỘI, BẢO HIỂM Y TẾ";
            this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            // 
            // frm_intokhaibhxh
            // 
            this.ClientSize = new System.Drawing.Size(271, 165);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.txt_hoten);
            this.Controls.Add(this.txt_manv);
            this.Controls.Add(this.lst_bhxh);
            this.Controls.Add(this.butThoat);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.butIn);
            this.Controls.Add(this.butXem);
            this.ForeColor = System.Drawing.Color.DarkBlue;
            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
            this.Name = "frm_intokhaibhxh";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "In tờ khai BHXH, BHYT";
            this.Load += new System.EventHandler(this.frm_intokhaibhxh_Load);
            this.ResumeLayout(false);
            this.PerformLayout();

		}
		#endregion


        private void frm_intokhaibhxh_Load(object sender, EventArgs e)
        {           
            m_user = mDB.user;
            ngay = DateTime.Now.Day.ToString().PadLeft(2, '0');
            thang = DateTime.Now.Month.ToString().PadLeft(2, '0');
            nam = DateTime.Now.Year.ToString();
            ngayin = lan.Change_language_MessageText("Ngày ") + ngay + lan.Change_language_MessageText(" tháng ")+thang + lan.Change_language_MessageText(" năm ") + nam;
            
            ds_hoten = load_hoten();
            lst_bhxh.DataSource = ds_hoten.Tables[0];
            lst_bhxh.DisplayMember = "manv";
            lst_bhxh.ValueMember = "hoten";
        }
        private void butThoat_Click(object sender, System.EventArgs e)
        {
            this.Close();
        }
        private DataSet load_hoten()
        {
            string sql = "SELECT manv,ho||' '||ten AS hoten from " + mDB.user + ".llnv order by manv";
            return mDB.get_data(sql);
        }
       
		private void butXem_Click(object sender, System.EventArgs e)
		{          
			print(1);
		}
		private void butIn_Click(object sender, System.EventArgs e)
		{			
            print(1);
		}	
		private void cbobBoPhan_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if (e.KeyCode==Keys.Enter)
				SendKeys.Send("{Tab}{F4}");
		}
		private void dgrXemLyLich_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if (e.KeyCode.ToString() == "Enter")
				SendKeys.Send("{Tab}{F4}");
		}		

    	private void print(int prn)
		{           
            ngay = DateTime.Now.Day.ToString().PadLeft(2, '0');
            thang = DateTime.Now.Month.ToString().PadLeft(2, '0');
            nam = DateTime.Now.Year.ToString();
            s_mmyy = thang + nam.Substring(2, 2);
			try
			{
                sql = " select ll.manv,trim(ll.ho)||' '||trim(ll.ten) as hoten,tb.tentt,hd.hdldso";
                sql +=" ,case when ll.phai='Nam' then 'X' else '' end nam";
                sql +=" , case when ll.phai<>'Nam' then 'X' else '' end nu";
                sql +=" ,to_char(ll.ngaysinh,'dd/mm/yyyy') as ngaysinh, dt.tendt,qg.tenqg as quocgia,ll.quequan as nguyenquan";
                sql +=" , ll.diachi ||', '||qh.tenquan ||', '||tt.tentt as diachi_nv,cd.ten as chuyenmon";
                sql += " ,hd.loaihd,mh.tenhd,to_char(hd.ngayhd,'dd/mm/yyyy') as ngayhd,to_char(hd.tungay,'dd/mm/yyyy') as ngayhieuluc";
                sql +=" ,ll.cmndso,to_char(ll.cmndngay,'dd/mm/yyyy') as cmndngay,ll.noicapcmnd,the.sobhyt,hd.sobhxh";
                sql += " from " + mDB.user + ".llnv ll inner join " + mDB.user + ".hopdong hd on hd.manv = ll.manv";
                sql +=" left join "+mDB.user+".thebhyt the on the.manv= ll.manv";
                sql += " left join " + mDB.user + ".quocgia qg on qg.maqg=ll.maqg ";
                sql += " left join " + mDB.user + ".dantoc dt on dt.madt = ll.madt";
                sql += " left join " + mDB.user + ".btdquan qh on qh.maqu=ll.h_diachi";
                sql += " left join " + mDB.user + ".btdtt tt on tt.matt = ll.t_diachi";
                sql += " left join " + mDB.user + ".v_tinhbhyt tb on ll.manv=tb.manv";
                sql += " left join " + mDB.user + ".mahd mh on hd.loaihd=mh.mahd";
                sql += " inner join " + mDB.user + ".max_lan ml on hd.manv=ml.manv";
                sql += " left join "+mDB.user+".dmcd cd on ll.macd=cd.ma";
                sql +=" where hd.ngaynghi is null and to_timestamp(hd.ngaydenct,'dd-mm-yyyy') <= to_timestamp('"+ngay+"-"+thang+"-"+nam+"','dd-mm-yyyy') ";
                sql += " and hd.lan=ml.lan and ll.manv = '"+ txt_manv.Text.Trim() +"'";                

                sql2 = " select ll.manv,to_char(cv.ngayhl,'mm/yyyy') as ngayhl,to_char(cv.denngay,'mm/yyyy') as denngay,cv.nghenghiep,cv.donvi,";
                sql2 += " cv.mucluong as heso,cv.hs_chucvu,cv.tn_vuotkhung,cv.tn_nghe,cv.hs_khac";
                sql2 += " from " + mDB.user + ".llnv ll left join " + mDB.user + ".congviec cv on cv.manv=ll.manv";
                sql2 += " inner join " + mDB.user + ".hopdong hd on hd.manv = ll.manv ";
                sql2 += " inner join " + mDB.user + ".max_lan ml on hd.manv=ml.manv";
                sql2 += " where hd.ngaynghi is null";
                sql2 += " and hd.lan=ml.lan ";
                if (txt_manv.Text!="") 
					sql2 +=" and ll.manv='"+ txt_manv.Text.Trim() +"'";	
                
                sql2 += " order by ll.manv, cv.ngayhl desc";
                sql2 += " limit (10)";  
				ds=mDB.get_data(sql,sql2);
                ds.WriteXml("..\\xml\\h_ToKhaiBHYT_BHXH.xml", XmlWriteMode.WriteSchema);
                if (ds.Tables[1].Rows.Count < 10)
                {
                    DataRow nrow;
                    for (int i = ds.Tables[1].Rows.Count + 1; i <= 10; i++)
                    {
                        nrow = ds.Tables[1].NewRow();
                        nrow["manv"] = "";
                        ds.Tables[1].Rows.InsertAt(nrow, i);
                    }
                }
				if (ds.Tables[0].Rows.Count>0)
				{

                    switch (prn)
                    {
                        case 1: 
                            frmReport f = new frmReport(mDB, ds, "", "h_ToKhaiBHYT_BHXH.rpt",user_dn);
                            f.ShowDialog();                         
                            break;                        
                    }              
					
				}
				else 
				{
					MessageBox.Show(lan.Change_language_MessageText("Chưa có đầy đủ dữ liệu"),AccessData.Msg);					
					
				}
			}
			catch{}
		}

        private void txt_manv_KeyDown(object sender, KeyEventArgs e)
        {
            if ((e.KeyCode == Keys.Down) || (e.KeyCode == Keys.Up))
            {
                this.lst_bhxh.Focus();
            }
            else if (e.KeyCode == Keys.Return)
            {
                if (this.lst_bhxh.Visible)
                {
                    this.lst_bhxh.Focus();
                }
                else
                {
                    SendKeys.Send("{Tab}");
                }
            }
        }

        private void txt_manv_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!IsNumeric(e.KeyChar.ToString()))
                e.Handled = true;
            if (e.KeyChar == 8 || e.KeyChar == '.')
                e.Handled = false;
        }
        private bool IsNumeric(string strNum)
        {
            int iNum;
            try
            {
                iNum = int.Parse(strNum);
                return true;
            }
            catch
            {
                return false;
            }
        }

        private void txt_hoten_KeyDown(object sender, KeyEventArgs e)
        {
            if ((e.KeyCode == Keys.Down) || (e.KeyCode == Keys.Up))
            {
                this.lst_bhxh.Focus();
            }
            else if (e.KeyCode == Keys.Return)
            {
                if (this.lst_bhxh.Visible)
                {
                    this.lst_bhxh.Focus();
                }
                else
                {
                    SendKeys.Send("{Tab}");
                }
            }
        }

        private void txt_hoten_TextChanged(object sender, EventArgs e)
        {
            try
            {
                if (this.ActiveControl == this.txt_hoten)
                {
                    this.loc_hoten(txt_hoten.Text.Trim());
                    this.lst_bhxh.BrowseToDanhmuc(txt_hoten, txt_manv, txt_hoten, 60);
                }
            }
            catch { };
        }
        private void loc_hoten(string hoten)
        {
            try
            {
                CurrencyManager cm = (CurrencyManager)this.BindingContext[lst_bhxh.DataSource];
                DataView dv = (DataView)cm.List;
                dv.RowFilter = "hoten like '%" + hoten.Trim() + "%' ";
            }
            catch
            {

            }
        }
        DataRow[] dr_manv;
        private void txt_manv_Validated(object sender, EventArgs e)
        {


            if (txt_manv.Text.Trim() != "")
            {
                this.dr_manv = ds_hoten.Tables[0].Select("manv='" + txt_manv.Text.Trim() + "'");
                if (dr_manv != null)
                {
                    txt_hoten.Text = dr_manv[0]["hoten"].ToString().Trim();
                }
                else
                {
                    txt_hoten.Text = "";
                }
            }
        }

        private void lst_bhxh_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Return)
            {
                txt_manv_Validated(null, null);
            }
        }    
	}
}